﻿using System;
using System.Text.RegularExpressions;

namespace Unikado.PHPLibs.Tokenizer
{

    internal static class ParseHelper
    {

        #region - - - - - -   P R I V A T E   S T A T I C   F I E L D S   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

        private static readonly char[] simpleWordStartChars =
        {
            'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
            'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','_'
        };
        private static readonly char[] wordStartChars =
        {
            '\\',
            'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
            'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','_'
        };
        private static readonly char[] wordChars =
        {
            '\\',
            'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
            'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
            '0','1','2','3','4','5','6','7','8','9','_'
        };
        private static readonly char[] numCharsSimple = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
        private static readonly char[] numChars = { '.', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
        private static readonly char[] hexChars =
        {
            'A', 'B', 'C', 'D', 'E', 'F', 'a', 'b', 'c', 'd', 'e', 'f', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
        };
        private static readonly Regex simpleWordRx = new Regex("^[A-Za-z_][A-Za-z0-9_]*$", RegexOptions.Compiled);
        private static readonly Regex intOctRegex = new Regex("^0[0-7]+$", RegexOptions.Compiled);
        private static readonly Regex dfnRx = new Regex("^(function|class|interface)$", RegexOptions.Compiled | RegexOptions.IgnoreCase);
        private static readonly char[] brackets = { '{', '(', '[', ']', ')', '}' };

        #endregion

        #region - - - - - -   P U B L I C   S T A T I C   M E T H O D S   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

        public static bool IsBracket(char c)
        {
            return Array.IndexOf(brackets, c) > -1;
        }
        public static bool IsSimpleWordStartChar(char c)
        {
            return Array.IndexOf(simpleWordStartChars, c) > -1;
        }
        public static bool IsWordStartChar(char c)
        {
            return Array.IndexOf(wordStartChars, c) > -1;
        }
        public static bool IsWordChar(char c)
        {
            return Array.IndexOf(wordChars, c) > -1;
        }
        public static bool IsNumCharSimple(char c)
        {
            return Array.IndexOf(numCharsSimple, c) > -1;
        }
        public static bool IsNumChar(char c)
        {
            return Array.IndexOf(numChars, c) > -1;
        }
        public static bool IsHexChar(char c)
        {
            return Array.IndexOf(hexChars, c) > -1;
        }
        public static bool IsSimpleWord(string str)
        {
            return simpleWordRx.IsMatch(str);
        }
        public static bool IsOctInteger(string str)
        {
            return intOctRegex.IsMatch(str);
        }
        public static bool IsDefinition(string str)
        {
            return dfnRx.IsMatch(str);
        }

        #endregion

    }

}